package com.mark.mall.admin.platform.controller;


import com.github.pagehelper.PageInfo;
import com.mark.mall.admin.platform.dto.PageParamDTO;
import com.mark.mall.admin.platform.dto.ProductDTO;
import com.mark.mall.admin.platform.dto.ProductStockDTO;
import com.mark.mall.admin.platform.dto.param.ProductAddParam;
import com.mark.mall.admin.platform.dto.param.ProductUpdateParam;
import com.mark.mall.admin.platform.entity.Product;
import com.mark.mall.admin.platform.service.ProductService;
import com.mark.mall.common.resultBean.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * <p>
 * 物料表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-02-28
 */
@RestController
@RequestMapping("/admin/product")
@Transactional
@Api(tags = "后台物料管理模块")
@RequiredArgsConstructor
public class ProductController {
    private final ProductService productService;

    @ApiOperation(value = "后台获取物料列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "code",
                    value = "物料编码",
                    dataType = "string"),
    })
    @GetMapping("list")
    public R<PageInfo<ProductDTO>> productPageInfo(PageParamDTO pageParamDTO, String code) {
        return new R<>(productService.getProductPage(pageParamDTO.getPageNo(), pageParamDTO.getPageSize(), code));
    }

    @ApiOperation(value = "物料新增", httpMethod = "POST")
    @PostMapping("add")
    public R<Boolean> addProduct(@RequestBody @Valid ProductAddParam param) throws Exception {
        return productService.addProduct(param);
    }

    @ApiOperation(value = "物料删除(可批量操作)", httpMethod = "POST")
    @PostMapping("del")
    public R<Boolean> delProduct(String ids) throws Exception {
        return productService.delProduct(ids);
    }

    @ApiOperation(value = "物料上下架(可批量操作)", httpMethod = "POST")
    @PostMapping("shelf")
    public R<Boolean> delProduct(String ids, String shelfState) throws Exception {
        return productService.shelfProduct(ids, shelfState);
    }

    @ApiOperation(value = "物料修改(只能修改code,价格,名称)", httpMethod = "POST")
    @PostMapping("update")
    public R<Boolean> updateProduct(@RequestBody @Valid ProductUpdateParam param) throws Exception {
        return productService.updateProduct(param);
    }

    @ApiOperation(value = "根据物料id获取物料详情", httpMethod = "GET")
    @GetMapping("productById")
    public R<Product> productById(String id) {
        return new R<>(productService.getProductById(id));
    }

    @ApiOperation(value = "物料关联库存", httpMethod = "GET")
    @GetMapping("stockOfProduct")
    public R<PageInfo<ProductStockDTO>> productStockPageInfo(PageParamDTO pageParamDTO, String code) {
        return productService.productStockPageInfo(pageParamDTO, code);
    }

    @ApiOperation(value = "物料出库", httpMethod = "POST")
    @PostMapping("openStock")
    public R<Boolean> openStock(String id, int openNum) throws Exception{
        return productService.openStock(id, openNum);
    }

    @ApiOperation(value = "物料入库", httpMethod = "POST")
    @PostMapping("inStock")
    public R<Boolean> inStock(String id, int inNum) throws Exception{
        return productService.inStock(id, inNum);
    }

    @ApiOperation(value = "物料主图操作", httpMethod = "POST")
    @PostMapping("productMailImg")
    public R<Boolean> productMailImg(MultipartFile file, String id) throws Exception {
        return productService.productMailImg(file, id);
    }

}
